<template>
  <div v-loading="loading">
    <h1>菜单管理</h1>
    <el-button type="primary" @click="handleToInsert">新增</el-button>
    <el-button type="danger" @click="handleDelete">删除</el-button>
    <!-- ///////////////////添加/////////////////////   -->
    <el-dialog :title="this.titles" :visible.sync="MenuInsertdialogForm" :before-close="handleClose">
      <el-form :model="MenuInsertform">
        <el-form-item label="类型" :label-width="MenuformLabelWidth">
          <el-radio-group v-model="MenuInsertform.type" @change="handleType()">
            <el-radio :label="0">目录</el-radio>
            <el-radio :label="1">菜单</el-radio>
            <el-radio :label="2">按钮</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="菜单名称" :label-width="MenuformLabelWidth">
          <el-input v-model="MenuInsertform.name" autocomplete="off" placeholder="菜单名称和按钮名称" />
        </el-form-item>
        <el-form-item label="上级菜单" :label-width="MenuformLabelWidth">
          <el-input v-model="MenuInsertform.parentId" autocomplete="off" readonly placeholder="0" />
        </el-form-item>
        <el-form-item label="菜单URL" :label-width="MenuformLabelWidth">
          <el-input v-model="MenuInsertform.url" autocomplete="off" placeholder="菜单URL" />
        </el-form-item>
        <el-form-item label="授权标识" :label-width="MenuformLabelWidth">
          <el-input v-model="MenuInsertform.perms" autocomplete="off" placeholder="多个用逗号分隔，如:user:list,user:create" />
        </el-form-item>
        <el-form-item label="排序号" :label-width="MenuformLabelWidth">
          <el-input v-model="MenuInsertform.orderNum" autocomplete="off" />
        </el-form-item>
        <el-form-item label="图标" :label-width="MenuformLabelWidth">
          <el-input v-model="MenuInsertform.icon" autocomplete="off" placeholder="菜单图标"  @focus="innerVisible=true"/>
          <el-dialog
            append-to-body
            title="ICON"
            width="80%"
            :visible.sync="innerVisible">
            <div class="icons-container">
              <el-tabs type="border-card">
                <el-tab-pane label="Icons">
                  <div class="grid">
                    <div v-for="item of svgIcons" :key="item" @click="handleClipboard(generateIconCode(item),$event)">
                      <el-tooltip placement="top">
                        <div slot="content">
                          {{ generateIconCode(item) }}
                        </div>
                        <div class="icon-item">
                          <svg-icon :icon-class="item" class-name="disabled" />
                          <span>{{ item }}</span>
                        </div>
                      </el-tooltip>
                    </div>
                  </div>
                </el-tab-pane>
                <el-tab-pane label="Element-UI Icons">
                  <div class="grid">
                    <div v-for="item of elementIcons" :key="item" @click="handleClipboard(generateElementIconCode(item),$event)">
                      <el-tooltip placement="top">
                        <div slot="content">
                          {{ generateElementIconCode(item) }}
                        </div>
                        <div class="icon-item">
                          <i :class="'el-icon-' + item" />
                          <span>{{ item }}</span>
                        </div>
                      </el-tooltip>
                    </div>
                  </div>
                </el-tab-pane>
              </el-tabs>
            </div>
            <span slot="footer" class="dialog-footer"></span>
          </el-dialog>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="MenuInsertdialogForm = false">取 消</el-button>
        <el-button type="primary" @click="handleInsertSave">确 定</el-button>
      </div>
    </el-dialog>
    <!-- ///////////////////添加/////////////////////   -->

    <!-- ///////////////////修改/////////////////////   -->
    <el-dialog :title="this.titles" :visible.sync="MenudialogUpdateForm" :before-close="handleClose">
      <el-form :model="MenuUpdateform">
        <el-form-item label="类型" :label-width="MenuformLabelWidth">
          <el-radio-group v-model="MenuUpdateform.type" @change="handleTypee()">
            <el-radio :label="0">目录</el-radio>
            <el-radio :label="1">菜单</el-radio>
            <el-radio :label="2">按钮</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="菜单名称" :label-width="MenuformLabelWidth">
          <el-input v-model="MenuUpdateform.name" autocomplete="off" placeholder="菜单名称和按钮名称" />
        </el-form-item>
        <el-form-item label="上级菜单" :label-width="MenuformLabelWidth">
          <el-input v-model="MenuUpdateform.parentId" autocomplete="off" readonly />
        </el-form-item>
        <el-form-item label="授权标识" :label-width="MenuformLabelWidth">
          <el-input v-model="MenuUpdateform.perms" autocomplete="off" placeholder="多个用逗号分隔，如:user:list,user:create" />
        </el-form-item>
        <el-form-item label="排序号" :label-width="MenuformLabelWidth">
          <el-input v-model="MenuUpdateform.orderNum" autocomplete="off" />
        </el-form-item>
        <el-form-item label="图标" :label-width="MenuformLabelWidth">
          <el-input v-model="MenuUpdateform.icon" autocomplete="off" placeholder="菜单图标"  @focus="innerVisible=true"/>
          <el-dialog
            append-to-body
            title="ICON"
            width="80%"
            :visible.sync="innerVisible">
            <div class="icons-container">
              <el-tabs type="border-card">
                <el-tab-pane label="Icons">
                  <div class="grid">
                    <div v-for="item of svgIcons" :key="item" @click="handleClipboard(generateIconCode(item),$event)">
                      <el-tooltip placement="top">
                        <div slot="content">
                          {{ generateIconCode(item) }}
                        </div>
                        <div class="icon-item">
                          <svg-icon :icon-class="item" class-name="disabled" />
                          <span>{{ item }}</span>
                        </div>
                      </el-tooltip>
                    </div>
                  </div>
                </el-tab-pane>
                <el-tab-pane label="Element-UI Icons">
                  <div class="grid">
                    <div v-for="item of elementIcons" :key="item" @click="handleClipboard(generateElementIconCode(item),$event)">
                      <el-tooltip placement="top">
                        <div slot="content">
                          {{ generateElementIconCode(item) }}
                        </div>
                        <div class="icon-item">
                          <i :class="'el-icon-' + item" />
                          <span>{{ item }}</span>
                        </div>
                      </el-tooltip>
                    </div>
                  </div>
                </el-tab-pane>
              </el-tabs>
            </div>
            <span slot="footer" class="dialog-footer"></span>
          </el-dialog>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="MenudialogUpdateForm = false">取 消</el-button>
        <el-button type="primary" @click="handleUpdateSave">确 定</el-button>
      </div>
    </el-dialog>
    <!-- ///////////////////修改/////////////////////   -->
    <el-table
      :data="tableData"
      style="width: 100%"
      row-key="menuId"
      border
      lazy
      :tree-props="{children: 'menuList', hasChildren: 'hasChildren'}"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column
        prop="menuId"
        label="菜单ID"
      />
      <el-table-column
        prop="name"
        label="菜单名称"
      />
      <el-table-column
        prop="icon"
        label="图标"
      />
      <el-table-column
        prop="type"
        label="类型"
      >
        <template v-slot="scope">
          <span v-if="scope.row.type==0">目录</span>
          <span v-if="scope.row.type==1">菜单</span>
          <span v-if="scope.row.type==2">按钮</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="orderNum"
        label="排序"
      />
      <     <el-table-column
      prop="url"
      label="菜单URL"
    />
      <el-table-column
        prop="perms"
        label="授权"
      />
      <el-table-column
        label="操作"
      >
        <template slot-scope="scope">
          <el-button type="success" size="small" @click="handleToUpdate(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import clipboard from '../../../../utils/clipboard'
import elementIcons from '../../../../views/icons/element-icons'

import svgIcons from "@/views/icons/svg-icons";
import axios from 'axios'
export default {
  name: 'MenuList',
  data() {
    return {
      svgIcons,
      elementIcons,
      innerVisible:false,
      loading:true,
      ids: [],
      tableData: [],
      titles: '',
      MenuInsertdialogForm: false,
      MenudialogUpdateForm: false,
      MenuInsertform: {
        name: '',
        parentId: '',
        type: '',
        url: '',
        perms: '',
        orderNum: '',
        icon: ''
      },
      MenuUpdateform: {
        type: '',
        name: '',
        parentId: '',
        perms: '',
        orderNum: '',
        icon: ''
      },
      MenuformLabelWidth: '120px'
    }
  },
  mounted() {
    this.initData()
  },
  methods: {
    handleOpenIconMenu(){
      this.innerVisible=true;
    },
    initData() {
      axios.post('http://localhost:8200/sys/menu/list').then((response) => {
        this.tableData = response.data.obj
        this.loading=false
      })
    },
    handleToInsert() {
      this.titles = '新增',
        this.MenuInsertdialogForm = true
    },
    handleToUpdate(row) {
      this.MenudialogUpdateForm = true,
        this.titles = '修改',
        this.MenuUpdateform = row
    },
    handleDelete() {
      if (this.ids.length == 0) {
        this.$message({
          message: '不能删除目录!',
          type: 'warning'
        })
        return
      }
      if (confirm('确认删除吗?')) {
        axios.get('http://localhost:8200/sys/menu/delete?ids=' + this.ids).then((response) => {
          if (response.data.code == 200) {
            alert(JSON.stringify(response.data.msg))
            this.initData()
          }
        })
      }
    },

    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
          this.MenuUpdateform={}
          this.MenuInsertform={}
        })
        .catch(_ => {});
    },
    handleSelectionChange(val) {
      this.ids = new Array()
      for (const i of val) {
        if (i.parentId != 0) {
          this.ids.push(i.menuId)
        }
      }
    },
    handleInsertSave() {
      axios.post('http://localhost:8200/sys/menu/save', this.MenuInsertform).then((response) => {
        if (response.data.code == 200) {
          alert(JSON.stringify(response.data.msg))
          this.MenuInsertdialogForm = false
          this.initData()
        }
      })
    },
    handleUpdateSave() {
      axios.post('http://localhost:8200/sys/menu/update', this.MenuUpdateform).then((response) => {
        if (response.data.code == 200) {
          alert(JSON.stringify(response.data.msg))
          this.MenudialogUpdateForm = false
          this.initData()
        }
      })
    },
    handleType() {
      this.MenuInsertform.parentId = this.MenuInsertform.type
    },
    handleTypee() {
      this.MenuUpdateform.parentId = this.MenuUpdateform.type
    },
    generateIconCode(symbol) {
      return symbol;
    },
    generateElementIconCode(symbol) {
      return "el-icon-"+symbol;
    },
    handleClipboard(text, event) {
      clipboard(text, event)
      this.MenuUpdateform.icon = text
      this.MenuInsertform.icon = text
      this.innerVisible=false;
    }
  },
}
</script>


<style lang="scss" scoped>
.icons-container {
  margin: 10px 20px 0;
  overflow: hidden;

  .grid {
    position: relative;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  }

  .icon-item {
    margin: 20px;
    height: 85px;
    text-align: center;
    width: 100px;
    float: left;
    font-size: 30px;
    color: #24292e;
    cursor: pointer;
  }

  span {
    display: block;
    font-size: 16px;
    margin-top: 10px;
  }

  .disabled {
    pointer-events: none;
  }
}
</style>
